using System;
using System.Collections.Generic;
using System.Text;
using AF_ORM.CORE;
using System.Data;
using Bussiness.Entity;
using System.Data.SqlClient;
using log4net;
namespace Bussiness.Manager
{


    public class PT_ServiceListManager : Bussiness.ManagerBase.PT_ServiceListManagerBase
    {
        private static readonly ILog _logger = LogManager.GetLogger(typeof(PT_ServiceListManager).Name);
        public static PT_ServiceListManager CreateInstant()
        {
            return new PT_ServiceListManager();
        }

        public DataTable GetServiceList()
        {
            try
            {
                string sql = "select ServiceCode,servicecode + '-' + serviceName  as ServiceName from PT_ServiceList where isdeleted = 0";
                return SqlHelper.ExecuteDataTable(Config.ConnectionConfig.ConnectionString, CommandType.Text, sql);
            }
            catch (Exception ex)
            {
                _logger.Debug(ex.Message);
                return new DataTable();
            }
        }
        public PT_ServiceListEntity GetByID(int ID)
        {
            try
            {
                List<PT_ServiceListEntity> lst = SelectByIdLST(ID);
                if (lst != null && lst.Count > 0)
                    return lst[0];
                return null;
            }
            catch (Exception ex)
            {
                _logger.Debug(ex.Message);
                return null;
            }
        }
        public PT_ServiceListEntity GetByServiceCode(string ServiceCode)
        {
            List<PT_ServiceListEntity> lst = SelectByServiceCodeLST(ServiceCode);
            if (lst != null && lst.Count > 0)
                return lst[0];
            return null;
        }
        public bool DeleteAll()
        {
            try
            {
                string sql = "Delete From PT_ServiceList";
                if (SqlHelper.ExecuteNonQuery(Config.ConnectionConfig.ConnectionString, CommandType.Text, sql) > 0)
                {
                    sql = "Delete From PT_Service_Model";
                    if (SqlHelper.ExecuteNonQuery(Config.ConnectionConfig.ConnectionString, CommandType.Text, sql) > 0)
                        return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                _logger.Debug(ex.Message);
                return false;
            }
        }
        public DataTable GetAll()
        {
            try
            {
                string sql = @"SELECT s.*,sm.ModelCode, sm.AgentPrice,sm.Price
                           FROM PT_ServiceList s INNER JOIN PT_Service_Model sm ON s.ServiceCode= sm.ServiceCode AND s.IsDeleted=0 
                           ORDER BY s.ServiceCode";
                return SqlHelper.ExecuteDataTable(Config.ConnectionConfig.ConnectionString, CommandType.Text, sql);
            }
            catch (Exception ex)
            {
                _logger.Debug(ex.Message);
                return new DataTable();
            }
        }
        public DataTable GetAll2()
        {
            try
            {
                string sql = @"SELECT s.*
                           FROM PT_ServiceList s 
                           ORDER BY s.ServiceCode";
                return SqlHelper.ExecuteDataTable(Config.ConnectionConfig.ConnectionString, CommandType.Text, sql);
            }
            catch (Exception ex)
            {
                _logger.Debug(ex.Message);
                return new DataTable();
            }
        }
        public DataTable GetByService_Model(string serviceCode, string ModelCode)
        {
            try
            {
                string sql = @"SELECT s.*,sm.ModelCode, sm.AgentPrice,sm.Price
                           FROM PT_ServiceList s INNER JOIN PT_Service_Model sm ON s.ServiceCode= sm.ServiceCode AND s.IsDeleted=0 
                           WHERE sm.ServiceCode=@ServiceCode AND sm.ModelCode=@ModelCode
                           ORDER BY s.ServiceCode";
                SqlParameter[] param = new SqlParameter[2];
                param[0] = new SqlParameter("@ServiceCode", serviceCode);
                param[1] = new SqlParameter("@ModelCode", ModelCode);
                return SqlHelper.ExecuteDataTable(Config.ConnectionConfig.ConnectionString, CommandType.Text, sql, param);
            }
            catch (Exception ex)
            {
                _logger.Debug(ex.Message);
                return new DataTable();
            }
        }
        public DataTable SearchByServiceAndModel(string serviceCode, string ModelCode)
        {
            try
            {
                string sql = "SearchServiceList";
                SqlParameter[] param = new SqlParameter[2];
                param[0] = new SqlParameter("@serviceCode", serviceCode);
                param[1] = new SqlParameter("@modelCode", ModelCode);
                return SqlHelper.ExecuteDataTable(Config.ConnectionConfig.ConnectionString, CommandType.StoredProcedure, sql, param);
            }
            catch (Exception ex)
            {
                _logger.Debug(ex.Message);
                return new DataTable();
            }
        }
    }
}

